package com.lagou.controller;/*
 *Author:cxz
 *@Date:${Date}
 */

import com.lagou.DubboApiService;
import com.lagou.dao.CodeMapper;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
@ClassName CodeControlelr

*@Description
*@Author cxz
*@Date 2020/7/8 18:56
*Version V1.0
*/
@RestController
@RequestMapping("/code")
public class CodeControlelr {
    @Autowired
    private CodeMapper codeMapper;

  /*  @Autowired
    private UserFegin userFegin;*/
    @Reference
    private DubboApiService dubboApiService;
    @RequestMapping("/create/{email}")
    public Boolean sendEmai(@PathVariable("email") String email) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < 6; i++) {
            Integer random = getRandom(0, 9);
            stringBuilder.append(random);
        }
        String o = dubboApiService.sendEmail(email, stringBuilder.toString());
        System.out.println("--------------"+o);
       // codeMapper.saveCode(email, stringBuilder.toString());
        return true;
    }


    @RequestMapping("/validate/{emal}/{code}")
    public Integer validate(@PathVariable("email") String email, @PathVariable("code") String code) {
        String codeByEmail = codeMapper.getCodeByEmail(email);
        if (StringUtils.isEmpty(codeByEmail)) {
            return 2;
        }
        if (code.equals(codeByEmail)) {
            return 1;
        }
        return 0;
    }

    public Integer getRandom(int start, int end) {
        int max = 0;
        int min = 0;
        if (start >= end) {
            max = start;
            min = end;
        } else {
            max = end;
            min = start;
        }
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }
}
